package com.nbsaas.boot.app.ext.resource;

import com.nbsaas.boot.app.api.domain.request.AppRoleRequest;
import com.nbsaas.boot.app.api.domain.response.AppRoleResponse;
import com.nbsaas.boot.app.data.entity.AppRole;
import com.nbsaas.boot.app.data.repository.AppRoleRepository;
import com.nbsaas.boot.app.ext.apis.AppRoleExtApi;
import com.nbsaas.boot.rest.response.ResponseObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;


@Service
public class AppRoleExtResource implements AppRoleExtApi {

    @Resource
    private AppRoleRepository appRoleRepository;

    @Override
    public ResponseObject<AppRoleResponse> updateBasic(AppRoleRequest request) {
        ResponseObject<AppRoleResponse> result=new ResponseObject<>();
        Optional<AppRole> res = appRoleRepository.findById(request.getId());
        if (!res.isPresent()){
            return  ResponseObject.error("角色不存在");
        }
        AppRole role=res.get();
        role.setName(request.getName());
        role.setRemark(request.getRemark());
        role.setBillViewPeriod(request.getBillViewPeriod());
        role.setReportViewPeriod(request.getReportViewPeriod());
        role.setLastDate(new Date());
        appRoleRepository.save(role);
        return result;
    }
}
